<template>
<!--pages/my/money_recharge.wxml-->
<view class="container">
    <form bindsubmit="formSubmit">
        <view class="modify">
            <input type="digit" :placeholder="placeholder" name="money" focus="true" bindfocus="inputBg" bindblur="inputBg" :style="'background:' + inputBg"></input>
        </view>
        <view class="remind"><lang keywords="金额最多仅支持两位小数"></lang></view>
        <button class="block_btn mlr20" formType="submit"><lang keywords="充值"></lang></button>
    </form>
</view>

</template>

"<script>
// pages/my/money_recharge.js
import common from '../../utils/common.js'
var app = getApp();

export default {
  data() {
    return {
      inputBg: 'white'
    };
  },

  onUnload: function () {// 页面关闭
  },
  onHide: function () {// 页面隐藏
  },
  onShow: function () {// 页面显示
  // #ifdef H5
  common.hideWxShare()
  // #endif
  },
  onReady: function () {// 页面渲染完成
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var _this = this;

    common.getTxtKey('请填写充值金额').then(key => {
      _this.setData({
        placeholder: key
      });
    }, () => {});
  },
  components: {},
  props: {},
  methods: {
    inputBg: function () {
      this.setData({
        inputBg: this.inputBg == 'white' ? '#ebebeb' : 'white'
      });
    },
    formSubmit: function (e) {
      console.log(e.detail);

      if (isNaN(e.detail.value.money) || e.detail.value.money == '') {
        wx.showModal({
          title: common.changeTxt('提示'),
          content: common.changeTxt("请输入合法的金额"),
          showCancel: false
        });
        return false;
      } else if (e.detail.value.money > 10000) {
        wx.showModal({
          title: common.changeTxt('提示'),
          content: common.changeTxt("单次充值金额最高不能超过1万元"),
          showCancel: false
        });
        return false;
      } else if (e.detail.value.money < 0.01) {
        wx.showModal({
          title: common.changeTxt('提示'),
          content: common.changeTxt("单次充值金额最低不能低于 0.1 元"),
          showCancel: false
        });
        return false;
      }

      common.post('Recharge&a=index', {
        money: e.detail.value.money
      }, "saveResult", this);
    },
    saveResult: function (result) {
      wx.hideToast();
      wx.navigateTo({
        url: "../pay/index?type=" + result.type + '&order_id=' + result.order_id
      });
    },
    setData: function (obj) {
      let that = this;
      let keys = [];
      let val, data;
      Object.keys(obj).forEach(function (key) {
        keys = key.split('.');
        val = obj[key];
        data = that.$data;
        keys.forEach(function (key2, index) {
          if (index + 1 == keys.length) {
            that.$set(data, key2, val);
          } else {
            if (!data[key2]) {
              that.$set(data, key2, {});
            }
          }

          data = data[key2];
        });
      });
    }
  }
};
</script>
<style>
/* pages/my/money_recharge.css */
.modify{ padding: 10rpx 20rpx; background: #fff; border-bottom: #f1f1f1 1rpx solid;}
.modify input{ font-size: 30rpx; height: 28rpx;  padding:10rpx;  }
.remind{ padding:30rpx 20rpx 0 20rpx; line-height: 1;}
</style>